<script setup lang="ts">
import router from '@/router';
import { reactive, computed } from 'vue';
import { login } from '@/request/userRequest';
import { useUserStore } from '@/stores/user';
import { getErrorData } from '@/tools/api_tool';
import { judgeDataIsNull } from '@/tools/normal';
import { ElMessage } from 'element-plus';

const userStore = useUserStore();
const form = reactive({
  userAccount: null,
  password: null,
  errorMessage: ''
})

const disabled = computed(() => {
  return judgeDataIsNull(form.userAccount) || judgeDataIsNull(form.password)
})


const onSubmit = function () {
  login(form.userAccount, form.password).then((result) => {
    ElMessage.success('登录成功');
    userStore.setToken(result.data.data);
    router.push('/')
  });
}

const onCancel = function () {
  router.push('/');
}
</script>

<template>
  <main>
    <el-form :model="form" label-width="120px" class="my_form">
      <el-form-item label="用户名" required="true">
        <el-input v-model="form.userAccount" style="width: var(--my_form_input_width);" />
      </el-form-item>
      <el-form-item label="密码" required="true">
        <el-input v-model="form.password" type="password" style="width: var(--my_form_input_width);" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit" :disabled="disabled">登录</el-button>
        <el-button @click="onCancel">取消</el-button>
      </el-form-item>
      <div style="color:red">
        {{ form.errorMessage }}
      </div>
    </el-form>
  </main>
</template>
